package com.ruoyi.web.scheduled;

import com.ruoyi.web.domain.vo.GangdanAppraiseVo;
import com.ruoyi.web.mapper.GangDanConfirmMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * description:
 * auth fcb
 * date 2025-04-07 17:03
 **/
@Component
public class WorkFormStatusTask {

    @Autowired
    private GangDanConfirmMapper gangDanConfirmMapper;

    //@Scheduled(cron ="0 */1 * * * ?")
    @Scheduled(cron ="0 0 2 * * ?")
    public void workFormStatusTask() {
        List<GangdanAppraiseVo> list = gangDanConfirmMapper.queryNoEvaList();
        if(CollectionUtils.isEmpty(list)) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        list.forEach(item -> {
            Date completeTime = item.getCompleteTime();
            if(completeTime != null && this.after7Days(completeTime, now)) {
                gangDanConfirmMapper.updateWorkFormEva(item.getBaseId(), "满意");
            }
        });
    }

    private boolean after7Days(Date completeTime, LocalDateTime now) {
        LocalDateTime targetDateTime = completeTime.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime().plusDays(7);
        return now.isAfter(targetDateTime);
    }
}
